<?php
namespace manage;

require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/Lib/Page.php';

global $config_item;

use Workerman\Protocols\Http;
if(!Http::sessionStarted()) Http::sessionStart();
checkLogin();

// 权限检查
$funcs = getFuncs($_SESSION['admin_group']);
if(!in_array(33, $funcs)) {
	echo '<script type="text/javascript">location.href="dashboard.php";</script>';
	#Http::header('Location:dashboard.php');
	return;
}

$error = $_GET['error'] ?? null;
$success = $_GET['success'] ?? null;
$now = time();
$today = date('Y-m-d');

global $game_redis;
global $OPERATION_EXCHANGE, $OPERATION_ITEM;

$TABLE_TYPE = [1001=>'万炮桌', 1002=>'五万炮桌', 1003=>'十万炮桌'];
$TABLE_DIFF = [0=>'最容易', 1=>'容易', 2=>'普通', 3=>'困难', 4=>'最困难'];

$kc_cfg = KEY['crystal_table_cfg'];
$kc_list = KEY['crystal_table_list'];
$table_play = KEY['analytic'];
$table_num = KEY['table'];

$action = $_GET['action'] ?? null;
if($action == 'newtable') {
    $type = $_POST['type'];
    $dif  = $_POST['diff'];
    if(!isset($TABLE_TYPE[$type])) {
		echo '<script type="text/javascript">location.href="game_endless_table.php?error=新增桌失败. 无效的桌类型";</script>';
		return;
    }
    if(!isset($TABLE_DIFF[$dif])) {
		echo '<script type="text/javascript">location.href="game_endless_table.php?error=新增桌失败. 无效的难度";</script>';
		return;
    }

    $get_max_table_id = \Core\Cache::get('game', 'CRYSTAL_MAX_TABLE_ID', false);
    if($get_max_table_id < 10001) {
	    $max_table_id = 10001;
	    $table_num = 1;
	    \Core\Cache::set('game', 'CRYSTAL_MAX_TABLE_ID', $max_table_id, false);
    } else {
	    $max_table_id = intval($get_max_table_id) + 1;
	    $table_num = $max_table_id - 10000;
	    \Core\Cache::incr('game', 'CRYSTAL_MAX_TABLE_ID', 1);
    }

    $table_id = 'CRYSTAL_' . $type . '_' . $max_table_id;
    $table_info = [
        'num'    => $table_num,
        'status' => 'normal',
        'dif'    => $dif,
    ];
	$find_table = \Core\Cache::hget($kc_cfg['instance'], $kc_cfg['pre'].$table_id, 'status', $kc_cfg['encode']);
	if(isset($find_table['status'])) {
		echo '<script type="text/javascript">location.href="game_endless_table.php?error=新增桌失败. 该桌号已存在";</script>';
		return;
	}
	try {
		\Core\Cache::zadd($kc_list['instance'], $kc_list['pre'] . $type, $table_id, $max_table_id);
		\Core\Cache::hmset($kc_cfg['instance'], $kc_cfg['pre'] . $table_id, $table_info, $kc_cfg['encode']);
	} catch(\Exception $e) {
		echo '<script type="text/javascript">location.href="game_endless_table.php?error=新增桌失败. 系统异常";</script>';
    }
	echo '<script type="text/javascript">location.href="game_endless_table.php?success=新增桌成功";</script>';
    return;
}

display_header();
?>

<section class="content" style="">

	<?php display_title('游戏管理 - 无尽海域 桌管理'); ?>

	<?php if($error) : ?>
		<div class="alert alert-warning alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
			<span><?php echo $error; ?></span>
		</div>
	<?php endif ?>
	<?php if($success) : ?>
		<div class="alert alert-success alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
			<h4><i class="icon fa fa-check"></i> 成功</h4>
			<?php echo $success; ?>
		</div>
	<?php endif ?>

	<div class="box box-primary" style="margin-top:5px;">
		<div class="box-header with-border" header_name="game_info">
			<div class="col-sm-6 tt" style="font-size:18px;text-align:left;padding-left:0;">
				<i class="fa fa-list"></i> 桌列表

				<button class="btn btn-primary" style="display:inline-block; margin-left:15px;" data-toggle="modal" data-target="#modal-add-table">新增桌</button>
			</div>
			<div style="text-align:right">
			</div>
		</div>
		<div class="box-body">
			<table class="table table-bordered table-striped dataTable table-hover">
				<tr>
					<th width="60">桌号</th>
					<th width="100">桌ID</th>
					<th width="150">难度</th>
					<th width="150">桌类型</th>
					<th width="80">在线人数</th>
					<th width="130">总玩</th>
					<th width="130">总得</th>
					<th width="130">系统总盈亏</th>
					<th>操作</th>
				</tr>
				<?php
                $table_keys = [];
				foreach($TABLE_TYPE as $type_id=>$type_name) {
					$get_tables = \Core\Cache::zrange($kc_list['instance'], $kc_list['pre'].$type_id, false);
					if(!empty($get_tables)) {
					    $table_keys = array_merge($table_keys, $get_tables);
                    }
                }
				$tables = [];
				foreach($table_keys as $table_key => $_nothing) {
				    $get_table_data = \Core\Cache::hgetall($kc_cfg['instance'], $kc_cfg['pre'].$table_key, $kc_cfg['encode']);
				    if(isset($get_table_data['status'])) {
				        $tables[$table_key] = $get_table_data;
                    }
				    $play_info = \Core\Cache::hgetall($table_play['instance'],$table_play['pre'].$table_key, $kc_cfg['encode']);
                    $tables[$table_key]['_CrystalTotalPlay'] = $play_info['_CrystalTotalPlay'];
                    $tables[$table_key]['_CrystalTotalWin'] = $play_info['_CrystalTotalWin'];
                    $table_nums = \Core\Cache::hgetall($table_num['instance'],$table_num['pre'].$table_key, $table_num['encode']);
                    $tables[$table_key]['online'] = $table_nums['sitdownNum'];
                }
				foreach($tables as $tid => $tinfo) {
				    $tarr = explode('_', $tid);
				?>
                    <td><?php echo $tinfo['num']; ?></td>
                    <td><?php echo $tarr[2]; ?></td>
                    <td><?php echo $tinfo['dif'] . ' - ' . $TABLE_DIFF[$tinfo['dif']]; ?></td>
                    <td><?php echo $tarr[1] . ' - ' . $TABLE_TYPE[$tarr[1]]; ?></td>
                    <td><?php echo $tinfo['online'] ?? 0; ?></td>
                    <td><?php echo $tinfo['_CrystalTotalPlay']; ?></td>
                    <td><?php echo $tinfo['_CrystalTotalWin']; ?></td>
                    <td><?php echo $tinfo['_CrystalTotalPlay'] - $tinfo['_CrystalTotalWin']; ?></td>
					<td>
						<button class="btn btn-xs btn-info" disabled>玩家列表</button> |
                        <button class="btn btn-xs btn-primary" disabled>参数设定</button> |
                        <button class="btn btn-xs btn-warning" disabled>账目清零</button> |
                        <button class="btn btn-xs btn-warning" disabled>冻结</button> |
                        <button class="btn btn-xs btn-danger" disabled>删除</button>
					</td>
				<?php
					echo '</tr>';
				}
				?>
			</table>
		</div>
	</div>

    <div class="modal fade" id="modal-add-table">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form action="game_endless_table.php?action=newtable" method="post" style="margin:0;padding:0;">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">新增桌</h4>
                    </div>
                    <div class="modal-body">
                        <table class="table table-bordered table-striped dataTable table-hover">
                            <tr>
                                <th>类型</th>
                                <td>
                                    <?php foreach($TABLE_TYPE as $tt => $tn) : ?>
                                    <label><input required type="radio" name="type" class="minimal" value="<?php echo $tt; ?>" /> <?php echo $tn; ?></label>
                                    &nbsp;&nbsp;
                                    <?php endforeach ?>
                                </td>
                            </tr>
                            <tr>
                                <th>难度</th>
                                <td>
	                                <?php foreach($TABLE_DIFF as $tt => $tn) : ?>
                                        <label><input required type="radio" name="diff" class="minimal" value="<?php echo $tt; ?>" /> <?php echo $tn; ?></label>
                                        &nbsp;&nbsp;
	                                <?php endforeach ?>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary" id="add_table_submit">确定</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</section>

<?php footer_load_js(); ?>

<script type="text/javascript">
    $(function() {
        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);
    });
</script>

</body>
</html>

